var planId = getQueryString("id");
var devId = getQueryString("devId");
$(function () {
    $("#jqGrid").Grid({
        url: '../devmainbaseam/listAllByDev?lockFlag=1&devId=' + devId,
        width: 1600,
        colModel: [
            {label: '选项编码', name: 'code', width: 120},
            {label: '选项名称', name: 'name', index: 'NAME', width: 120},
            {label: '设备类型', name: 'devTypeName', index: 'DEV_TYPE_NAME', width: 150},
            {label: '供应厂家', name: 'devSupplyName', index: 'DEV_SUPPLY', width: 150},
            {label: '维护内容', name: 'checkContent', index: 'CHECK_CONTENT', width: 210},
            {label: '检查标准', name: 'checkSt', index: 'CHECK_ST', width: 210},
            {label: '物料需求', name: 'materialRequ', index: 'MATERIAL_REQU', width: 120,
                formatter: function (value) {
                    return value?value:'\\';
                }
            },
            {label: '方法手段', name: 'methodType', index: 'METHOD_TYPE', width: 120},
            {label: '生产', name: 'checkDatePrud', index: 'CHECK_DATE_PRUD', width: 90},
            {label: '非生产', name: 'checkDateUprud', index: 'CHECK_DATE_UPRUD', width: 90},
            {
                label: '实施频率', name: 'freRate', index: 'FRE_RATE', width: 120,
                formatter: function (value) {
                    return sysEnums.FRERATE[value];
                }
            },
            {label: '人次', name: 'perEv', index: 'PER_EV', width: 90},
            {label: '选项备注', name: 'remark', index: 'REMARK', width: 160},
            {name: 'devType', hidden: true},
            {name: 'id', key: true, hidden: true}
        ]
    });
    vm.getSelectTreeShow();
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "typeCode",
            pIdKey: "typePid",
            rootPId: -1
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    },
    callback: {
        beforeCheck: true,
    }

};

//类型树-首页加载增加查询方法
var typeSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "typeCode",
            pIdKey: "typePid",
            rootPId: -1
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    },
    callback: {
        beforeCheck: true,
        beforeClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    //此处treeNode 为当前节点
    var str = '';
    str = getAllChildrenNodes(treeObj, str);
    // 加上被选择节点自己
    str = str + ',' + treeObj.typeCode;
    // 去掉最前面的逗号
    var ids = str.substring(1, str.length);

    vm.q.devType = ids;
    vm.query();
}


// 递归，获取所有子节点
function getAllChildrenNodes(treeNode, result) {
    if (treeNode.isParent) {
        var childrenNodes = treeNode.children;
        if (childrenNodes) {
            for (var i = 0; i < childrenNodes.length; i++) {
                result += ',' + childrenNodes[i].typeCode;
                result = getAllChildrenNodes(childrenNodes[i], result);
            }
        }
    }
    return result;
}

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        devMainBase: {
            devTypeName: "",
            devType: "",
            planId: ""

        },
        q: {
            name: '',
            code: '',
            devType: '',
            devSupplyName: '' // 供应商名称
        },
        freRateArray: enumsToList("FRERATE2")
    },
    methods: {
        query: function () {
            vm.reload();
        },
        addPlan: function () {
            //获取选中数据集合
            var param = {"planId": planId, "mainBaseIdList": getSelectedRows("#jqGrid")};
            // 加载设备组织列表
            Ajax.request({
                url: "../devAmMaintenanceOption/batchSave",
                params: JSON.stringify(param),
                type: "POST",
                contentType: "application/json",
                successCallback: function (res) {
                    alert(res.msg, function (index) {
                        vm.reload();
                    });
                }
            });
        },
        viewAll: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 查看详情',
                content: '../device/devAmRulesView.html?plan_id=' + planId
            })
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                search: true,
                postData: {
                    'name': vm.q.name,
                    'code': vm.q.code,
                    'devType': vm.q.devType,
                    'devSupplyName': vm.q.devSupplyName
                },
                page: page
            }).trigger("reloadGrid", [{page: 1}]);
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                code: '',
                devType: '',
                devSupplyName: ''
            };
            vm.reload();
        },
        getSelectTreeShow: function () {
            //加载分类树
            Ajax.request({
                url: '../devtype/listAll',
                async: true,
                successCallback: function (r) {
                    deptZtree = $.fn.zTree.init($("#devTypeTree"), typeSetting, r.list);
                    // deptZtree.expandAll(true);
                }
            });
        },
    }
});
